package pt.ipleiria.plp.Lixeiras;

import java.rmi.RemoteException;
import pt.ipleiria.plp.stub.BlackSpot;
import pt.ipleiria.plp.stub.BlackSpotState;
import pt.ipleiria.plp.stub.OperationStatus;
import pt.ipleiria.plp.stub.SpottingSoap_Stub;

/**
 * Class used when editing a Lixeira already sent
 * @author Filipe Felisberto e Ricardo Silva
 */
public class LixeiraEdited extends LixeiraPeddingComplete {

    private boolean pendingAllFotosDelete = false;

    public LixeiraEdited(String guid, String title, double latitude, double longitude,
            pt.ipleiria.plp.stub.WasteVolume quantity, java.lang.String[] classification,
            int estimatedNumberVolunteers, String estimatedWasteVolume, String notes,
            java.lang.String[] tags, LixeiraDate inputDate,
            pt.ipleiria.plp.stub.BlackSpotState status, boolean pendingAllFotosDelete) {

        super(guid, title, latitude, longitude, quantity, classification, estimatedNumberVolunteers,
                estimatedWasteVolume, notes, tags, inputDate, new LixeiraDate(), status);

    }

    public LixeiraEdited(LixeiraSent lixeira, double latitude, double longitude,
            pt.ipleiria.plp.stub.WasteVolume quantity, java.lang.String[] classification,
            int estimatedNumberVolunteers, String estimatedWasteVolume, String notes,
            java.lang.String[] tags, boolean pendingAllFotosDelete) {

        super(lixeira.getGuid(), lixeira.getTitle(), latitude, longitude, quantity,
                classification, estimatedNumberVolunteers, estimatedWasteVolume,
                notes, tags, new LixeiraDate(lixeira.getBs().getInputDate()),
                new LixeiraDate(), BlackSpotState.fromString(lixeira.getState()));


        this.pendingAllFotosDelete = pendingAllFotosDelete;

    }

    public LixeiraSendResult send(String app) {

        for (int i = 0; i < getClassification().length; i++) {
            System.out.println("CLasss:" + getClassification()[i]);
        }

        BlackSpot bs = new BlackSpot(getLatitude(), getLongitude(), getQuantity(),
                getClassification(), getEstimatedNumberVolunteers(),
                getEstimatedWasteVolume(), getNotes(), getTitle(), getTags(),
                getInputDate().getXmlDate(), getLastEditDate().getXmlDate(),
                getStatus(), getGuid());

        SpottingSoap_Stub service = new SpottingSoap_Stub();

        try {
            System.out.println("id-" + getGuid());
            System.out.println("app-" + app);

            OperationStatus ops = service.editBlackSpot(getGuid(), bs, app);

            if (ops.isOperationSuceeded()) {
                LixeiraSent lix = toSentStatus();
                if (getImages() != null && getImages().length > 0) {

                    String error = sendImages(getGuid(), app, service);

                    if (error != null) {

                        return new LixeiraSendResult(lix, error);
                    }

                }

                return new LixeiraSendResult(lix);
            } else {
                return new LixeiraSendResult(ops.getErrorMessage());
            }

        } catch (RemoteException ex) {
            ex.printStackTrace();
            return new LixeiraSendResult(ex.getMessage());
        }
    }

    /**
     * @return the pendingAllFotosDelete
     */
    public boolean isPendingAllFotosDelete() {
        return pendingAllFotosDelete;
    }

    /**
     * @param pendingAllFotosDelete the pendingAllFotosDelete to set
     */
    public void setPendingAllFotosDelete(boolean pendingAllFotosDelete) {
        this.pendingAllFotosDelete = pendingAllFotosDelete;
    }

    public String delete(String app) {

        SpottingSoap_Stub service = new SpottingSoap_Stub();

        deleteImages(service, app);

        OperationStatus ops = null;
        try {
            ops = service.deleteBlackSpot(getGuid(), app);
        } catch (RemoteException ex) {
            ex.printStackTrace();
            return "Comunication Error";
        }

        if (ops.isOperationSuceeded()) {
            return null;
        } else {
            return ops.getErrorMessage();
        }
    }

    /**
     * Deletes all the images associated with this lixeira, the web service doesn't
     * support deleting one by one.
     * @param app the app key
     * @return null if it worked or the error message
     */
    private String deleteImages(SpottingSoap_Stub service, String app) {

        try {

            OperationStatus ops = service.deleteAllMediaObjects(getGuid(), app);

            if (ops.isOperationSuceeded()) {
                return null;
            } else {
                return ops.getErrorMessage();
            }

        } catch (RemoteException ex) {
            ex.printStackTrace();
            return ex.getMessage();
        }

    }
}
